<?php
namespace Framework\Facades;

class Facade
{
    /**
     * @var 被外观化的应用程序实例
     */
    protected static $app = [
    	'request' => \Framework\Request::class
    ];

	/**
	 * @var 解析的对象实例
	 */
    protected static $resolvedInstance;

    /**
     * 获取门面后面的根对象。
     */
    public static function getFacadeRoot()
    {
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    }


    protected static function resolveFacadeInstance($name)
    {
    	//如果是已经实例化过的对象直接返回
        if (is_object($name)) {
            return $name;
        }

        //如果是已经实例化过 返回单例
        if (isset(static::$resolvedInstance[$name])) {
            return static::$resolvedInstance[$name];
        }


        if (static::$app) {
        	if( is_object(static::$app[$name]) ){
            	return static::$resolvedInstance[$name] = static::$app[$name];

        	}else{
        		$_class = static::$app[$name];
            	return static::$resolvedInstance[$name] = new $_class();
        	}
        }
    }


    public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();

        if (! $instance) {
            throw new RuntimeException('尚未设置外观根');
        }

        return $instance->$method(...$args);
    }


}